package com.wyp.demo.net;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.wyp.demo.R;
import com.wyp.demo.utils.ADUtils;

public class WifiTestActivity extends Activity {
	private List<Button> btns = new ArrayList<Button>();
	private WifiAdminManager wifiAdminManager;
	private ListView cfgview;
	private ListView scanview;
	
	private List<String> cfglist = new ArrayList<String>();
	private List<String> scanlist = new ArrayList<String>();
	
	private ArrayAdapter<String> cfgAdapter = null;
	private ArrayAdapter<String> scanAdapter = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.wifitest);
		LinearLayout linearLayout = (LinearLayout)findViewById(R.id.linearLayout5);
		for (int i = 0; i < 5; i++) {
			Button btn = new Button(this);
			btn.setText("Button-"+i);
			btn.setTag(i);
			btn.setOnClickListener(new MyBtnClick());
			btns.add(btn);
			linearLayout.addView(btn);
		}
		wifiAdminManager = new WifiAdminManager(this);
		
		cfgview = (ListView)findViewById(R.id.listView1);
		cfgview.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> adapterview, View view,
					int i, long l) {
				try {
					TextView txt = (TextView)view;
					String ssid = (String) txt.getText();
					System.out.println(ssid);
					List<WifiConfiguration> res = wifiAdminManager.getConfiguration();
					for (WifiConfiguration wifiConfiguration : res) {
						String key = wifiConfiguration.SSID;
						if(ssid.equals(key)) {
							//System.out.println(wifiConfiguration.BSSID);
							System.out.println(wifiConfiguration.hiddenSSID);
							System.out.println(wifiConfiguration.networkId);
							System.out.println(wifiConfiguration.preSharedKey);
							System.out.println(wifiConfiguration.priority);
							System.out.println(wifiConfiguration.SSID);
							System.out.println(wifiConfiguration.status);
							System.out.println(wifiConfiguration.wepTxKeyIndex);
							System.out.println(wifiConfiguration.allowedAuthAlgorithms);
							System.out.println(wifiConfiguration.allowedGroupCiphers);
							System.out.println(wifiConfiguration.allowedKeyManagement);
							System.out.println(wifiConfiguration.allowedPairwiseCiphers);
							System.out.println(wifiConfiguration.allowedProtocols);
							//wifiAdminManager.getmWifiManager().disableNetwork(wifiConfiguration.networkId);
							//wifiAdminManager.getmWifiManager().removeNetwork(wifiConfiguration.networkId);
						}
					}
					//wifiAdminManager.getmWifiManager().removeNetwork(netId)
				} catch (Exception e) {
					e.printStackTrace();
				}
				
			}
			
		});
		scanview = (ListView)findViewById(R.id.listView2);
		scanview.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> adapterview, View view,
					int i, long l) {
				try {
					TextView txt = (TextView)view;
					String ssid = (String) txt.getText();
					System.out.println(ssid);
					List<ScanResult> res = wifiAdminManager.getWifiList();
					for (ScanResult scanResult : res) {
						String key = scanResult.SSID;
						if(ssid.equals(key)) {
							System.out.println(scanResult.BSSID);
							System.out.println(scanResult.capabilities);
							System.out.println(scanResult.frequency);
							System.out.println(scanResult.level);
							System.out.println(scanResult.describeContents());
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
		});
		
		cfgAdapter = new ArrayAdapter<String>(WifiTestActivity.this, android.R.layout.simple_expandable_list_item_1, cfglist);
		scanAdapter = new ArrayAdapter<String>(WifiTestActivity.this, android.R.layout.simple_expandable_list_item_1, scanlist);
		
		cfgview.setAdapter(cfgAdapter);
		scanview.setAdapter(scanAdapter);
	}

	private void testCheckState() {
		/*
			WIFI_STATE_DISABLING = 0;
			WIFI_STATE_DISABLED = 1; 
			WIFI_STATE_ENABLING = 2; 
			WIFI_STATE_ENABLED = 3;  
			WIFI_STATE_UNKNOWN = 4;  
		 */
		int state = wifiAdminManager.checkState();
		System.out.println(""+state);
		ADUtils.showBugMsg(this, ""+state);
	}
	
	private void testScanWifi() {
		scanlist.clear();
		wifiAdminManager.startScan();
		List<ScanResult> res = wifiAdminManager.getWifiList();
		if(res == null || res.size() == 0)
			return;
		for (ScanResult scanResult : res) {
			scanlist.add(scanResult.SSID);
		}
		scanAdapter.notifyDataSetChanged();
	}
	
	private void testCfgWifi() {
		cfglist.clear();
		List<WifiConfiguration> res = wifiAdminManager.getConfiguration();
		if(res == null || res.size() == 0)
			return;
		for (WifiConfiguration wifiConfiguration : res) {
			cfglist.add(wifiConfiguration.SSID);
		}
		cfgAdapter.notifyDataSetChanged();
	}
	private void testOpenWifi() {
		wifiAdminManager.openWifi();
	}
	public class MyBtnClick implements OnClickListener {
		public void onClick(View v) {
			Integer choice = (Integer)v.getTag();
			switch (choice) {
			case 0:
				testCheckState();
				break;
			case 1:
				testScanWifi();
				break;
			case 2:
				testCfgWifi();
				break;
			case 3:
				testOpenWifi();
				break;
			default:
				break;
			}
		}
	}
}
